package com.android.accessibility.provider;

import java.io.File;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;

import com.android.accessibility.utils.Log;

public class DBHelper extends SQLiteOpenHelper {

    private static final String CREATE_NICKLIST =
            "CREATE TABLE IF NOT EXISTS " + DBConstant.TABLE_NICKLIST
            + "("
            + DBConstant._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + DBConstant.NICK_NAME + " TEXT ,"
            + DBConstant.NICK_NAME_HASH + " TEXT ,"
            + DBConstant.NICK_TYPE + " INTEGER,"
            + DBConstant.NICK_FUNS + " INTEGER,"
            + DBConstant.NICK_TYPELABEL + " TEXT,"
            + DBConstant.NICK_FUNSLABEL + " TEXT,"
            + DBConstant.OP_TIME + " LONG,"
            + DBConstant.IS_DELETE + " INTEGER DEFAULT 0,"
            + DBConstant.FOO + " text"
            + ")";

    private static final String DROP_BLACKLIST = "DROP TABLE " + DBConstant.TABLE_NICKLIST + " IF EXISTS";
    private static final String CREATE_BLACK_UNIQUE_INDEX =
            "CREATE UNIQUE INDEX nick_index ON " + DBConstant.TABLE_NICKLIST 
            + "(" + DBConstant.NICK_NAME + "," + DBConstant.NICK_TYPE + ");";

    private static final String DROP_BLACK_UNIQUE_INDEX = "DROP INDEX nick_index";

    private Context mContext;
    public DBHelper(Context context) {
        super(context,getMyDatabaseName(context), null, DBConstant.DB_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            db.execSQL(CREATE_NICKLIST);
            db.execSQL(CREATE_BLACK_UNIQUE_INDEX);
        }catch(SQLException e){
            Log.d(Log.TAG, "error : " + e);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(newVersion > oldVersion){
            try {
                db.execSQL(DROP_BLACKLIST);
                db.execSQL(DROP_BLACK_UNIQUE_INDEX);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                onCreate(db);
            }
        }
    }

    private static String getMyDatabaseName(Context context) {
        String databasename = DBConstant.DB_NAME;
        File downDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        String dataDir = null;
        if (downDir != null) {
            dataDir = new File(downDir, "data/" + context.getPackageName()
                    + "/databases/").getAbsolutePath();
        }
        if (dataDir == null) {
            dataDir = context.getFilesDir().getPath() + "/databases/";
        }
        File dbp = new File(dataDir);
        if (!dbp.exists()) {
            dbp.mkdirs();
        }
        File dbName = new File(dataDir, DBConstant.DB_NAME);
        databasename = dbName.getAbsolutePath();
        return databasename;
    }
}
